package com.dlc.shop.jky.common.foreign.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.dlc.shop.bean.model.ProdProp;
import com.dlc.shop.common.foreign.ipPortMap;
import com.dlc.shop.config.ShopConfig;
import com.dlc.shop.jky.common.foreign.service.ForeignSpecService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
public class ForeignSpecServiceImpl implements ForeignSpecService {

    private final ShopConfig shopConfig;

    public String getUrlIpPort(){
        ipPortMap ipPortMap = shopConfig.getForeignUrl();
        return ipPortMap.getUrl();
    }
    @Override
    public String save(ProdProp prodProp) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/spec/save";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(prodProp)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+urlIpPort+"返回数据---》"+response.body());
        return response.body();
    }

    @Override
    public String update(ProdProp prodProp) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/spec/update";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(prodProp)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+urlIpPort+"返回数据---》"+response.body());

        return response.body();
    }

    @Override
    public String delete(ProdProp prodProp) {
        //调用 https请求 链接进行同步数据
        // 构建body POST请求
        // 请求的URL
        String urlIpPort = getUrlIpPort();
        if (urlIpPort == null || urlIpPort.equals("")){
            return "";
        }
        String url = urlIpPort+ "/synchronize/spec/delete";

        // 构建POST请求
        HttpRequest request = HttpRequest.post(url).header("Content-Type", "application/json") // 设置请求头
                .body(JSON.toJSONString(prodProp)); // 设置请求体，这里是一个JSON字符串

        // 发送请求，并接收响应
        HttpResponse response = request.execute();

        // 打印响应的内容
        log.info("请求地址："+urlIpPort+"返回数据---》"+response.body());
        return response.body();

    }
}
